﻿using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Database.Control;
using Database.Model;
using System.Collections;

namespace ProjectZero
{
    public partial class CadastroClienteFiltro : MetroForm
    {
        public CadastroClienteFiltro()
        {
            InitializeComponent();
            tela();
        }

        private void tela()
        {
            try
            {
                Entities context = new Entities();

                //monta grid
                var v = Database.Control.Cliente.CadastroCliente.GridFiltroCliente();
                this.grdClientes.DataSource = v;

            }
            catch (Exception ex)
            {
                MessageBox.Show("Não foi possível carregaros os registros!\nDescrição do erro: " + ex, "Name Entry Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        
        }

        private void grdClientes_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            CadastroCliente t = new CadastroCliente(RetornaId(e), false);
            t.MdiParent = MdiParent; 
            t.Show();
        }

        private Int64 RetornaId(DataGridViewCellEventArgs e)
        {
            try
            {
                Int64 id = Convert.ToInt64(grdClientes.CurrentRow.Cells["id"].Value.ToString());
                return id;
            }
            catch
            {
                return 0;
            }
        }

        private void btnInserir_Click(object sender, EventArgs e)
        {
            inserir();
        }

        private void txtNome_Enter(object sender, EventArgs e)
        {
            txtNome.Text = string.Empty;
            txtNome.ForeColor = System.Drawing.Color.Black;
        }

        private void txtNome_Leave(object sender, EventArgs e)
        {
            txtNome.Text = "Buscar por nome";
            txtNome.ForeColor = System.Drawing.Color.Silver;
        }

        private void grdClientes_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (grdClientes.SelectedRows.Count == 1)
                    grdClientes.ClearSelection();

                int currentMouseOverRow = grdClientes.HitTest(e.X, e.Y).RowIndex;
                if (currentMouseOverRow < 0)
                    return;

                grdClientes.Rows[currentMouseOverRow].Selected = true;
                ContextMenu menu = new ContextMenu();
                MenuItem mnuExcluir = new MenuItem("Excluir");
                MenuItem mnuEditar = new MenuItem("Editar");
                MenuItem mnuImprimir = new MenuItem("Imprimir");

                menu.MenuItems.Add(mnuExcluir);
                menu.MenuItems.Add(mnuEditar);
                menu.MenuItems.Add(mnuImprimir);

                menu.Show(grdClientes, new Point(e.X, e.Y));
                //teste.Click += new EventHandler(this.qwe_click);
            }
        }

        private void CadastroClienteFiltro_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        private void grdClientes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
            else if (e.KeyCode == Keys.F1)
                inserir();
            else if (e.KeyCode == Keys.F5)
                tela();
        }

        private void inserir()
        {
            CadastroCliente c = new CadastroCliente(0, true);
            c.Show();
        }

        private void entra()
        { 
        
        }

        private void pcbAtualizar_Click(object sender, EventArgs e)
        {
            tela();
        }

    }
}
